import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const channelStore =createSlice({
    name:'channel',
    initialState:{
        channelList:[]
    },
    reducers: {
        setChannels(state,action){
            state.channelList = action.payload;
        }
    }
});
//发送异步请求
const {setChannels} = channelStore.actions;

const fetchChannlList = ()=>{
    // 需要return出去
    return async(dispatch)=>{
      const res = await axios.get('https://jsonplaceholder.typicode.com/posts');
      dispatch(setChannels(res.data))
    } 
}

export {fetchChannlList};
const reducer = channelStore.reducer;


export default reducer